home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / util / pack / xpk_Source.lha / xpk_Source / xpkmaster / libinit.c < prev    next >
C/C++ Source or Header  |  1998-03-27  |  7KB  |  258 lines

  1. #ifndef XPKMASTER_LIBINIT_C
  2. #define XPKMASTER_LIBINIT_C
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        libinit.c
  7.     Main:        xpkmaster
  8.     Versionstring:    $VER: libinit.c 1.0 (21.02.1998)
  9.     Author:        SDI
  10.     Distribution:    Freeware
  11.     Description:    all the library initialization stuff
  12.  
  13.  1.0   21.02.98 : first version, based on libdata.a and work of Gunther Nikl
  14. */
  15.  
  16. #include <proto/exec.h>
  17. #include <proto/locale.h>
  18. #include <exec/resident.h>
  19. #include <exec/initializers.h>
  20. #include "xpkmaster.h"
  21. #include "texts.h"
  22. #include "version.h"
  23.  
  24. struct LibInitData {
  25.  UBYTE i_Type;     UBYTE o_Type;     UBYTE  d_Type;    UBYTE p_Type;
  26.  UBYTE i_Name;     UBYTE o_Name;     STRPTR d_Name;
  27.  UBYTE i_Flags;    UBYTE o_Flags;    UBYTE  d_Flags;    UBYTE p_Flags;
  28.  UBYTE i_Version;  UBYTE o_Version;  UWORD  d_Version;
  29.  UBYTE i_Revision; UBYTE o_Revision; UWORD  d_Revision;
  30.  UBYTE i_IdString; UBYTE o_IdString; STRPTR d_IdString;
  31.  ULONG endmark;
  32. };
  33.  
  34. /************************************************************************/
  35.  
  36. LONG ReturnError(void);
  37. static void CloseLibraries(void);
  38. static void LocaleStrings(STRPTR *, ULONG, ULONG);
  39.  
  40. ULONG LibReserved(void);
  41. ASM(BPTR) LibExpunge(REG(a6, struct Library *));
  42. ASM(BPTR) LibClose(REG(a6, struct Library *));
  43. ASM(struct Library *) LibOpen(REG(a6, struct Library *));
  44. ASM(struct Library *) LibInit(REG(a0, BPTR), REG(d0, struct Library *));
  45.  
  46. /************************************************************************/
  47.  
  48. struct DosLibrary *        DOSBase        = 0;
  49. struct IntuitionBase *        IntuitionBase    = 0;
  50. struct UtilityBase *        UtilityBase    = 0;
  51. struct Library *        GadToolsBase    = 0;
  52. struct LocaleBase *        LocaleBase    = 0;
  53. struct Library *        XpkBase        = 0;
  54. static struct Catalog *     Catalog        = 0;
  55. static BPTR            SegList        = 0;
  56.  
  57. /************************************************************************/
  58.  
  59. /* First executable routine of this library; must return an error
  60.    to the unsuspecting caller */
  61. LONG ReturnError(void)
  62. {
  63.   return -1;
  64. }
  65.  
  66. /************************************************************************/
  67.  
  68. /* The mandatory reserved library function */
  69. ULONG LibReserved(void)
  70. {
  71.   return 0;
  72. }
  73.  
  74. /* Close the library, as called by CloseLibrary() */
  75. ASM(BPTR) LibClose(REG(a6, struct Library * xpkLib))
  76. {
  77.   if(!(--xpkLib->lib_OpenCnt))
  78.   {
  79.     if(xpkLib->lib_Flags & LIBF_DELEXP)
  80.       return LibExpunge(xpkLib);
  81.   }
  82.   return 0;
  83. }
  84.  
  85. /* Open the library, as called via OpenLibrary() */
  86. ASM(struct Library *) LibOpen(REG(a6, struct Library * xpkLib))
  87. {
  88.   /* Prevent delayed expunge and increment opencnt */
  89.   xpkLib->lib_Flags &= ~LIBF_DELEXP;
  90.   xpkLib->lib_OpenCnt++;
  91.  
  92.   return xpkLib;
  93. }
  94.  
  95. /* Expunge the library, remove it from memory */
  96. ASM(BPTR) LibExpunge(REG(a6, struct Library * xpkLib))
  97. {
  98.   if(!xpkLib->lib_OpenCnt)
  99.   {
  100.     CloseLibraries();
  101.  
  102.     /* Remove the library from the public list */
  103.     Remove((struct Node *) xpkLib);
  104.  
  105.     /* Free the vector table and the library data */
  106.     FreeMem((STRPTR) xpkLib - xpkLib->lib_NegSize, xpkLib->lib_NegSize +
  107.     xpkLib->lib_PosSize);
  108.  
  109.     return SegList;
  110.   }
  111.   else
  112.     xpkLib->lib_Flags |= LIBF_DELEXP;
  113.  
  114.   /* Return the segment pointer, if any */
  115.   return 0;
  116. }
  117.  
  118. /* Closes all the libraries opened by LibrarySetup() */
  119. static void CloseLibraries(void)
  120. {
  121.   if(LocaleBase)
  122.   {
  123.     if(Catalog)
  124.       CloseCatalog(Catalog);
  125.     CloseLibrary((struct Library *) LocaleBase);
  126.   }
  127.   if(GadToolsBase)
  128.     CloseLibrary(GadToolsBase);
  129.   if(UtilityBase)
  130.     CloseLibrary((struct Library *) UtilityBase);
  131.   if(IntuitionBase)
  132.     CloseLibrary((struct Library *) IntuitionBase);
  133.   if(DOSBase)
  134.     CloseLibrary((struct Library *) DOSBase);
  135. }
  136.  
  137. /* Initialize library */
  138. ASM(struct Library *) LibInit(REG(a0, BPTR seglist),
  139. REG(d0, struct Library * xpkLib))
  140. {
  141.   /* Remember the segment pointer */
  142.   SegList = seglist;
  143.  
  144.   /* get required data */
  145.  
  146.   if((DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37)))
  147.   {
  148.     if((IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37)))
  149.     {
  150.       if((UtilityBase = (struct UtilityBase *) OpenLibrary("utility.library", 37)))
  151.       {
  152.         if((GadToolsBase = OpenLibrary("gadtools.library", 37)))
  153.         {
  154.           if((LocaleBase = (struct LocaleBase *) OpenLibrary("locale.library", 38)))
  155.           {
  156.             if((Catalog = OpenCatalog(0, "xpkmaster.catalog",
  157.             OC_Version, 2, TAG_DONE)))
  158.             {
  159.           LocaleStrings(strings, LOCALE_STRINGSTART, LOCALE_STRINGCNT);
  160.           LocaleStrings(XpkErrs, LOCALE_ERRSTRINGSTART, LOCALE_ERRSTRINGCNT);
  161.         }
  162.           }
  163.           return (XpkBase = xpkLib);
  164.         }
  165.       }
  166.     }
  167.     CloseLibraries();
  168.   }
  169.  
  170.   /*
  171.   FreeMem((STRPTR) xpkLib - xpkLib->lib_NegSize, xpkLib->lib_NegSize +
  172.   xpkLib->lib_PosSize);
  173.   */
  174.  
  175.   return 0;
  176. }
  177.  
  178. static void LocaleStrings(STRPTR * string, ULONG start, ULONG count)
  179. {
  180.   while(count--)
  181.   {
  182.     *string = GetCatalogStr(Catalog, start++, *string);
  183.     ++string;
  184.   }
  185. }
  186.  
  187. /************************************************************************/
  188.  
  189. /* This is the table of functions that make up the library. The first
  190.    four are mandatory, everything following it are user callable
  191.    routines. The table is terminated by the value -1. */
  192.  
  193. static const APTR LibVectors[20] = {
  194.   LibOpen,
  195.   LibClose,
  196.   LibExpunge,
  197.   LibReserved,
  198.   LibReserved,
  199.   LIBXpkExamine,
  200.   LIBXpkPack,
  201.   LIBXpkUnpack,
  202.   LIBXpkOpen,
  203.   LIBXpkRead,
  204.   LIBXpkWrite,
  205.   LIBXpkSeek,
  206.   LIBXpkClose,
  207.   LIBXpkQuery,
  208.   LIBXpkAllocObject,
  209.   LIBXpkFreeObject,
  210.   LIBXpkPrintFault,
  211.   LIBXpkFault,
  212.   LIBXpkPassRequest,
  213.   (APTR)-1
  214. };
  215.  
  216. static const struct LibInitData LibInitData = {
  217.  0xA0, (UBYTE) OFFSET(Node,    ln_Type),      NT_LIBRARY,         0,
  218.  0x80, (UBYTE) OFFSET(Node,    ln_Name),      LIBNAME,
  219.  0xA0, (UBYTE) OFFSET(Library, lib_Flags),    LIBF_SUMUSED|LIBF_CHANGED, 0,
  220.  0x90, (UBYTE) OFFSET(Library, lib_Version),  VERSION,
  221.  0x90, (UBYTE) OFFSET(Library, lib_Revision), REVISION,
  222.  0x80, (UBYTE) OFFSET(Library, lib_IdString), IDSTRING,
  223.  0
  224. };
  225.  
  226. /* The following data structures and data are responsible for
  227.    setting up the Library base data structure and the library
  228.    function vector. */
  229.  
  230. static const ULONG LibInitTable[4] = {
  231.   (ULONG)sizeof(struct Library), /* Size of the base data structure */
  232.   (ULONG)LibVectors,             /* Points to the function vector */
  233.   (ULONG)&LibInitData,           /* Library base data structure setup table */
  234.   (ULONG)LibInit                 /* The address of the routine to do the setup */
  235. };
  236.  
  237. /************************************************************************/
  238.  
  239. /* The library loader looks for this marker in the memory
  240.    the library code and data will occupy. It is responsible
  241.    setting up the Library base data structure.
  242. */
  243.  
  244. static const struct Resident RomTag = {
  245.   RTC_MATCHWORD,                /* Marker value. */
  246.   (struct Resident *)&RomTag,   /* This points back to itself. */
  247.   (struct Resident *)&RomTag+1, /* This points behind this marker. */
  248.   RTF_AUTOINIT,                 /* The Library should be set up according to the given table. */
  249.   VERSION,                      /* The version of this Library. */
  250.   NT_LIBRARY,                   /* This defines this module as a Library. */
  251.   0,                            /* Initialization priority of this Library; unused. */
  252.   LIBNAME,                      /* Points to the name of the Library. */
  253.   IDSTRING,                     /* The identification string of this Library. */
  254.   (APTR)&LibInitTable           /* This table is for initializing the Library. */
  255. };
  256.  
  257. #endif /* XPKMASTER_LIBINIT_C */
  258.